package com.iwuke.dance.wallet.starlight_value;

import android.view.View;
import android.widget.TextView;

import com.iwuke.dance.R;
import com.iwuke.dance.activity.BaseActivity;
import com.iwuke.dance.gadget_view.DefaultView;
import com.iwuke.dance.gadget_view.TitleBarView;
import com.iwuke.dance.http_request.PathUrl;
import com.iwuke.dance.util.Util;
import com.iwuke.dance.util.merchants_connect.NetworkEvent;
import com.iwuke.dance.wallet.starlight_value.adapter.LiveDetailsIncomeAdapter;
import com.iwuke.dance.wallet.starlight_value.starlight_value_bean.StarlightTransactionRecordHomepageBean;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.RecyclerView;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * @author: 魏从响
 * @date: 2025/9/22
 * 直播详情收益
 */
public class LiveDetailsIncomeActivity extends BaseActivity implements View.OnClickListener {

    private TitleBarView title_bar;
    private RecyclerView recycler_view;
    private TextView tv_start_time;
    private TextView tv_end_time;
    private RefreshLayout refreshLayout;
    private int pageNum;
    private StarlightTransactionRecordHomepageBean data;
    private List<StarlightTransactionRecordHomepageBean> lists = new ArrayList<>();
    private LiveDetailsIncomeAdapter adapter;
    private DefaultView default_view;


    /**
     * @description 网络实时监听
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(NetworkEvent networkEvent){
        BaseActivity.setConnect(LiveDetailsIncomeActivity.this,networkEvent,title_bar);

    }

    @Override
    protected int getView() {
        return R.layout.activity_live_details_income;
    }

    @Override
    protected void init(View mView) {

        title_bar = findViewById(R.id.title_bar);
        //开始时间
        tv_start_time = findViewById(R.id.tv_start_time);
        //结束时间
        tv_end_time = findViewById(R.id.tv_end_time);

        //列表
        refreshLayout = findViewById(R.id.refresh_layout);
        recycler_view = findViewById(R.id.recycler_view);
        //默认视图
        default_view = findViewById(R.id.default_view);

        title_bar.setLeftOnClick(this);
        title_bar.setTitleText("直播收益明细");
        //开始时间
        tv_start_time.setOnClickListener(this);
        //结束时间
        tv_end_time.setOnClickListener(this);


    }

    @Override
    protected void initData() {

        data = Util.getSerializableSafe(getIntent(), "data", StarlightTransactionRecordHomepageBean.class);




        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
//                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                pageNum=1;
                starlightTransactionLiveRecord();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
//                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                pageNum++;
                starlightTransactionLiveRecord();
            }
        });

        pageNum=1;
        starlightTransactionLiveRecord();

    }

    private void starlightTransactionLiveRecord() {

        Map<String, String> map = new HashMap<>();
        map.put("starlightBalanceId", String.valueOf(data.id));//	星光值交易记录id	query	true integer(int64)
        map.put("page", String.valueOf(pageNum));//page	页码，从1开始,示例值(1)	query	false integer(int32)
        PathUrl.starlightTransactionLiveRecord(map, new PathUrl.DataCallBackList<StarlightTransactionRecordHomepageBean>() {
            @Override
            public void onSucessList(List<StarlightTransactionRecordHomepageBean> list) {
                Util.setRefreshlayout(refreshLayout);//下来加载 上拉刷新 没有更多数据了
                default_view.setVisibility(View.GONE);
                if (pageNum==1&&lists.size()>0){
                    lists.clear();
                    lists.addAll(list);
                }

                if (list==null||list.size()==0){
                    if (lists==null||lists.size()==0){
                        default_view.setTextView("没有相关记录~");
                        default_view.setVisibility(View.VISIBLE);
                    }
                    if (pageNum>1){//当前页没数据，页数减一
                        pageNum--;
                        refreshLayout.setNoMoreData(true);//没有更多数据了
                    }
                }

                if (adapter==null) {
                    setLinearLayoutManager(LiveDetailsIncomeActivity.this, recycler_view);
                    adapter = new LiveDetailsIncomeAdapter(LiveDetailsIncomeActivity.this, R.layout.adapter_live_details_income, lists);
                    recycler_view.setAdapter(adapter);
                }else {
                    if (list!=null&&list.size()>0){
                        // 首先，记录插入前列表的原始大小
                        int previousSize = adapter.getItemCount();
                        // 将新数据添加到原有列表的末尾
                        lists.addAll(list); // newDataList 是你本次加载得到的新数据列表
                        // 然后，通知适配器有新项从 previousSize 位置开始，插入了 newDataList.size() 个
                        adapter.notifyItemRangeInserted(previousSize, list.size());
                        refreshLayout.setEnableLoadMore(true);//有数据上拉加载成功
                    }

//                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFail(String errorCode) {
                Util.setRefreshlayout(refreshLayout);//下来加载 上拉刷新 没有更多数据了
                default_view.setTextView(errorCode);
                default_view.setVisibility(View.VISIBLE);
            }
        });

    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.img_left){
            finish();
        }if (v.getId()==R.id.tv_start_time||v.getId()==R.id.tv_end_time){
            Util.datePicker(LiveDetailsIncomeActivity.this);

            Util.setOnStartAndEnd(new Util.OnStartAndEnd() {
                @Override
                public void onStartAndEnd(Map<String, String> map) {
                    //开始时间
                    tv_start_time.setText(map.get("start"));
                    //结束时间
                    tv_end_time.setText(map.get("end"));
//                    pageNum=1;
//                    initRequest();
                }
            });
        }
    }
}
